#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#define g_SCREEN_WIDTH  800
#define g_SCREEN_HEIGHT  600

LPDIRECT3DDEVICE9 _Device;
LPD3DXSPRITE _Sprite_Handler;
LPDIRECT3DTEXTURE9 _Mario;

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

LPDIRECT3DTEXTURE9 LoadTexture (LPDIRECT3DDEVICE9 Device,char* filename,D3DCOLOR _Colorkey = D3DCOLOR_XRGB(255,0,255));
bool Init (HWND _hWnd);
void Draw ();

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT ("BeginingTexture");
    HWND         hwnd;
    MSG          msg;
    WNDCLASSEX   wndclassex = {0};
    wndclassex.cbSize        = sizeof(WNDCLASSEX);
    wndclassex.style         = CS_HREDRAW | CS_VREDRAW;
    wndclassex.lpfnWndProc   = WndProc;
    wndclassex.cbClsExtra    = 0;
    wndclassex.cbWndExtra    = 0;
    wndclassex.hInstance     = hInstance;
    wndclassex.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
    wndclassex.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wndclassex.lpszMenuName  = NULL;
    wndclassex.lpszClassName = szAppName;
    wndclassex.hIconSm       = wndclassex.hIcon;
	
    if (!RegisterClassEx (&wndclassex))
    {
        MessageBox (NULL, TEXT ("RegisterClassEx failed!"), szAppName, MB_ICONERROR);
        return 0;
    }
    hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW, 
		                  szAppName, 
        		          TEXT ("WindowTitle"),
                		  WS_OVERLAPPEDWINDOW,
		                  CW_USEDEFAULT, 
        		          CW_USEDEFAULT, 
                		  g_SCREEN_WIDTH, 
		                  g_SCREEN_HEIGHT, 
        		          NULL, 
                		  NULL, 
		                  hInstance,
        		          NULL); 
						  
    ShowWindow (hwnd, iCmdShow);
    UpdateWindow (hwnd);
	Init(hwnd);
	
	bool done = false;
	while(!done)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message==WM_QUIT) 
			{
				done=true;
			}
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		} 	
		Draw();
	}
    return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    switch (message)
    {
    case WM_CREATE:
        return (0);
		
    case WM_PAINT:
        hdc = BeginPaint (hwnd, &ps);
        TextOut (hdc, 0, 0, "A Window!", 27);
        EndPaint (hwnd, &ps);
        return (0);
		
    case WM_DESTROY:
        PostQuitMessage (0);
        return (0);
    }
    return DefWindowProc (hwnd, message, wParam, lParam);
}
bool Init(HWND _hWnd)
{
	LPDIRECT3D9 _pD3D;
	_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
	if (!_pD3D) 
	{
		MessageBox( NULL,"No compatible Direct3D object could be created.",
			"Fatal Error!",MB_OK | MB_ICONSTOP | MB_APPLMODAL );
		return false;
	}

	D3DPRESENT_PARAMETERS m_pD3DPP,mpp;

	ZeroMemory( &m_pD3DPP, sizeof(m_pD3DPP) );

	m_pD3DPP.BackBufferHeight			= g_SCREEN_HEIGHT;
	m_pD3DPP.BackBufferWidth			= g_SCREEN_WIDTH;
	m_pD3DPP.BackBufferFormat           = D3DFMT_A8R8G8B8;
	m_pD3DPP.BackBufferCount            = 1;
	m_pD3DPP.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
	m_pD3DPP.hDeviceWindow              = _hWnd;
	m_pD3DPP.Windowed                   = true;
	

	_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
		_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING,
		&m_pD3DPP, &_Device);
	if (_Device == NULL )
	{
		MessageBox(_hWnd,"Count Create device with D3DDEVTYPE_HAL & D3DCREATE_HARDWARE_VERTEXPROCESSING \n Try again !!! ","Warning!!",MB_OK);
		_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
			_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
			&m_pD3DPP, &_Device);
		if (_Device == NULL )
		{
			MessageBox(_hWnd,"Count Create device with D3DDEVTYPE_HAL & D3DCREATE_SOFTWARE_VERTEXPROCESSING /n Try again !!! ","Warning!!",MB_OK);
			_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF,
				_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
				&m_pD3DPP, &_Device);
			if (_Device == NULL )
			{
				MessageBox(_hWnd,"Count Create device with D3DDEVTYPE_REF & D3DCREATE_SOFTWARE_VERTEXPROCESSING /n Try again !!! ","Warning!!",MB_OK);
				_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_SW,
					_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
					&m_pD3DPP, &_Device);
				if (_Device == NULL )
				{
					MessageBox(_hWnd,"Count Create device with D3DDEVTYPE_SW & D3DCREATE_SOFTWARE_VERTEXPROCESSING /n Try again !!! ","Warning!!",MB_OK);
					return false;
				} 

			} 	

		} 	
	} 		

	D3DXCreateSprite(_Device,&_Sprite_Handler);
	_Mario = LoadTexture(_Device,"mario.png");

	return true;
}

void Draw()
{
	_Device->Clear(0,NULL,D3DCLEAR_TARGET,D3DXCOLOR(0,255,100,1),1.0f,0);

	_Device->BeginScene();
	_Sprite_Handler->Begin(D3DXSPRITE_ALPHABLEND);

	_Sprite_Handler->Draw(_Mario,NULL,NULL,&D3DXVECTOR3(100,100,0),D3DXCOLOR(1,1,1,1));


	RECT r;
	r.left = 50;
	r.right = 100;
	r.top = 0;
	r.bottom = 100;

	_Sprite_Handler->Draw(_Mario,&r,NULL,&D3DXVECTOR3(100,250,0),D3DXCOLOR(1,1,1,1));


	_Sprite_Handler->End();
	_Device->EndScene();



	_Device->Present(NULL,NULL,NULL,NULL);
}
LPDIRECT3DTEXTURE9 LoadTexture (LPDIRECT3DDEVICE9 Device,char* filename,D3DCOLOR _Colorkey)
{
	LPDIRECT3DTEXTURE9 _Image;
	D3DXIMAGE_INFO info; 
	HRESULT result; 
	result = D3DXGetImageInfoFromFile(filename,&info);
	if (result!=D3D_OK)
	{
		MessageBox(NULL,"Load info image failed!!!","Error",MB_OK);
		return NULL;
	}
	result = D3DXCreateTextureFromFileEx(
		_Device,
		filename,
		info.Width,
		info.Height,
		1,D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		_Colorkey,
		&info,
		NULL,
		&_Image);
	if (result!=D3D_OK) 
	{
		return NULL;
	}
	return _Image;
}